package com.uziot.security.uaa.controller;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.endpoint.TokenEndpoint;
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author shidt
 * @version V1.0
 * @className MyLoginController
 * @date 2020-08-24 22:21:50
 * @description
 */
@Slf4j
@RestController
public class MyLoginController {

    @Autowired
    private TokenEndpoint tokenEndpoint;

    @PostMapping(value = "/my/login")
    public Object login(@RequestBody UserVo user, HttpServletRequest request) throws Exception {
        log.info("登陆信息为：{}", JSON.toJSONString(user));

        Map<String, String> params = new HashMap<>(6);
        params.put("username", user.getUsername());
        params.put("password", user.getPassword());
        params.put("code", user.getCode());
        params.put("client_id", user.getClientId());
        params.put("client_secret", user.getClientSecret());
        params.put("grant_type", user.getGrantType());

        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(user.getClientId());


        UsernamePasswordAuthenticationToken authenticationToken
                = new UsernamePasswordAuthenticationToken(loginUser, null, null);
        authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));

        ResponseEntity<OAuth2AccessToken> responseEntity = tokenEndpoint.postAccessToken(authenticationToken, params);
        OAuth2AccessToken auth2AccessToken = responseEntity.getBody();
        log.info("用户登陆响应结果为：{}",JSON.toJSONString(auth2AccessToken));
        return responseEntity;

    }

    @Data
    public static class UserVo {
        private String username;
        private String password;
        private String code;
        private String clientId = "c1";
        private String clientSecret = "secret";
        private String grantType = "password";
    }


    @Data
    public static class LoginUser implements UserDetails {
        private String username;
        private String password;

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            return null;
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }
    }


}
